﻿Imports System.Data.SqlClient

Public Class DuplicataBR

    Private user As String
    Private userSite As String
    Private cn As CnData
    Private da As SqlDataAdapter
    Private ds As DataSet
    Private cmd As SqlCommand
    Private dtr As SqlDataReader

    Public Sub New(ByVal user As String, ByRef cn As CnData, ByVal userSite As String)
        ' Cet appel est requis par le concepteur.
        InitializeComponent()

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
        Me.user = user
        Me.cn = cn
        Me.userSite = userSite

    End Sub

    Private Sub Duplicata_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cn.Open()
        da = New SqlDataAdapter("SELECT '00' as NumSite,'' as NomDoc FROM Sites UNION SELECT NumSite,NomDoc FROM Sites WHERE Actif = 1", cn.getCn)
        ds = New DataSet
        da.Fill(ds, "SitesD")
        With siteD
            .DataSource = ds.Tables("SitesD")
            .DisplayMember = "NomDoc"
            .ValueMember = "NumSite"
            .SelectedValue = "00"
        End With
        Date1.Value = Date.Today.AddDays(-15)
        Date2.Value = Date.Today
        da = New SqlDataAdapter("SELECT TOP 200 Chargements.Chargement AS [N° de chargment], Chargements.DateExp AS [Date chargement], Sites.nomDoc AS De, Sites_1.nomDoc AS Vers, " _
                      & " Chargements.UtilExp AS Expéditeur, Chargements.UtilRec AS Réceptionneur, Chargements.DateRec AS [Date de réception], Chargements.Transporteur,  " _
                      & " Chargements.Chauffeur, Chargements.Immatriculation, Chargements.Plomb1 AS [Plomb n°1], Chargements.Plomb2 AS [Plomb n°2],  " _
                      & " Chargements.NbAgres AS [Nombre d'agrès], Chargements.TypeFlux AS [Type de flux], Chargements.Statut " _
                      & " FROM         Chargements INNER JOIN " _
                      & " Sites ON Chargements.Expediteur = Sites.NumSite INNER JOIN " _
                      & " Sites AS Sites_1 ON Chargements.Destinataire = Sites_1.NumSite " _
                      & " WHERE Chargements.Destinataire = '" & userSite & "' AND DateRec >= '" & Date1.Value & "' AND DateRec <= '" & Date2.Value & "' ORDER BY DateRec DESC", cn.getCn)
        'da.Fill(ds, "ListeC")

        If da.Fill(ds, "ListeC") >= 200 Then MsgBox("Seules les 200 dernières réceptions sont affichées")
        Liste.DataSource = ds.Tables("ListeC")

        cn.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not String.IsNullOrEmpty(Charg.Text) Then
            If Not IsNumeric(Charg.Text) Then
                MsgBox("Veuillez saisir un numero de chargement au format numérique")
                Exit Sub
            End If
        End If
        Dim f As String
        recherche.Text = Nothing
        f = " "
        
        If Not String.IsNullOrEmpty(Charg.Text) Then f = f & "AND Chargements.chargement like '%" & Charg.Text & "%' "
        If siteD.SelectedValue <> "00" Then f = f & "AND Chargements.Expediteur = '" & siteD.SelectedValue & "' "
        If Not String.IsNullOrEmpty(statut.Text) Then f = f & "AND Statut = '" & statut.Text & "'"

        da = New SqlDataAdapter("SELECT TOP 200 Chargements.Chargement AS [N° de chargment], Chargements.DateExp AS [Date chargement], Sites.nomDoc AS De, Sites_1.nomDoc AS Vers, " _
                      & " Chargements.UtilExp AS Expéditeur, Chargements.UtilRec AS Réceptionneur, Chargements.DateRec AS [Date de réception], Chargements.Transporteur,  " _
                      & " Chargements.Chauffeur, Chargements.Immatriculation, Chargements.Plomb1 AS [Plomb n°1], Chargements.Plomb2 AS [Plomb n°2],  " _
                      & " Chargements.NbAgres AS [Nombre d'agrès], Chargements.TypeFlux AS [Type de flux], Chargements.Statut " _
                      & " FROM         Chargements INNER JOIN " _
                      & " Sites ON Chargements.Expediteur = Sites.NumSite INNER JOIN " _
                      & " Sites AS Sites_1 ON Chargements.Destinataire = Sites_1.NumSite " _
                      & " WHERE Chargements.Destinataire = '" & userSite & "' AND DateRec >= '" & Date1.Value & "' AND DateRec <= '" & Date2.Value & "' " & f & " ORDER BY DateRec DESC", cn.getCn)
        ds.Tables("ListeC").Clear()
        If da.Fill(ds, "ListeC") >= 200 Then MsgBox("Seules les 200 dernières réceptions sont affichées")
        Liste.DataSource = ds.Tables("ListeC")
    End Sub

    Private Sub recherche_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles recherche.KeyPress
        If e.KeyChar = Chr(34) Or e.KeyChar = "'" Or e.KeyChar = "*" Then
            e.KeyChar = ""
            Exit Sub
        End If
    End Sub

    Private Sub recherche_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles recherche.TextChanged
        Dim retrdvview As New DataView
        With retrdvview
            .Table = ds.Tables("ListeC")
            .RowFilter = "[N° de chargment]+' '+[Date de réception]+' '+De like '%" & recherche.Text & "%'"
        End With
        Liste.DataSource = retrdvview
    End Sub

    Private Sub Fermer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fermer.Click
        Me.Close()
    End Sub


    Private Sub Liste_CellContentDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Liste.CellContentDoubleClick
        If e.RowIndex < 0 Then Exit Sub
        Dim BR As New ViewBR(Liste.Item(0, e.RowIndex).Value.ToString)
        Me.Cursor = Cursors.WaitCursor
        BR.ShowDialog()
        Me.Cursor = Cursors.Default
    End Sub
End Class